<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24" >
        <el-col :span="12" :offset="6">
          <div class="left-upload">
            <UploadImg ref="upload" @upload="onUpload"></UploadImg>
          </div>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" :loading="loading" @click="onSubmit('form')">保存
                </el-button>
              </div>
            </div>

          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>

import UploadImg from "@/components/UploadImg.vue";
import { registerAdminFace } from "@/api/user";

export default {
  components: {
    UploadImg
  },
  props: {
    adminId: {
      type: Number,
      default: () => {
        return 0;
      },
    },
  },
  mounted() {
  },
  data() {
    return {
      imageUrl: '',
      path: '',
      form: {
        typeId: 0,
        imageData: ''
      },
      loading: false,
    };
  },

  methods: {
    onUpload(e) {
      if (e.path && e.imageUrl) {
        this.imageUrl = e.imageUrl;
        this.path = e.path;
      }
    },
    async registerAdminFace() {
      let res = await registerAdminFace({
        imageData: this.imageUrl,
        adminId: this.adminId
      });

      if (res.code == 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });


        this.$emit("onClose", {
          isClose: true,
        });
      }

      this.loading = false;
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.registerAdminFace()
        }
      });
    },
    cancelModel() {
      this.$refs.upload.resetImage()
      this.$emit("onClose", {
        isClose: false,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}
.left-upload{
  display: flex;
  justify-content: center;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 160px;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}
</style>
